package ua.model;


import java.util.HashSet;

import ua.common.XmlManager;
import ua.util.Util; 
import ua.validator.Validator;

import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("user")
public class UserBean 
{
	@XStreamOmitField
	private String id;
	
	@XStreamAsAttribute
	private String username;
	
	@XStreamAsAttribute
	private String password;
	
	@XStreamAsAttribute
	@XStreamAlias("roles")
	private String strRoles = "";
	
	@XStreamOmitField
	private HashSet<RoleBean> roles;
	
	public UserBean() {
		this.username = "";
		this.password = "";
		this.id="";
		this.roles = new HashSet<RoleBean>();
	}

	public UserBean(String username, String password) {
		this.username = username;
		this.password = password;
		this.id="";
		this.roles = new HashSet<RoleBean>();
	}

	public String getId(){
		return id;
	} 
	
	public void setId(String userid){
		id=userid;
	}
	
	public void setUsername(String name) {
		this.username = name;
	}

	public String getUsername() {
		return username;
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setRolesStr(String strRoles) {
	    this.strRoles = strRoles;
    }

    public String getRolesStr() {
	    return strRoles;
    }

	public void setRoles(HashSet<RoleBean> roles) {
		this.roles = roles;
	}

	public HashSet<RoleBean> getRoles() {
		if (roles == null) {
			roles = new HashSet<RoleBean>();
		}
		return roles;
	}

	public RoleBean addRoleBean(RoleBean role) {
		getRoles().add(role);
		role.getUsers().add(this);
		addRoleStr(role);
		return role;
	}

	public void removeRoleBean(RoleBean role) {
		getRoles().remove(role);
		role.getUsers().remove(this);
		removeRoleStr(role);
	}
	
	/*ajout d'un user par name*/
	/*public RoleBean addRoleBean(String rolename) {
		return addRoleBean(getRoleByName(rolename));
	}
	
	public RoleBean getRoleByName(String Name) {
		for (RoleBean role : getRoles()) {
			if (role.getRolename().equals(Name)) {
				return role;
			}
		}
		return null;
	}*/
	
	public void addRoleStr(RoleBean role) {
		if (!strRoles.contains(role.getRolename())) {
			strRoles = Util.addString(strRoles, role.getRolename(),",");
		}
	}

	public void removeRoleStr(RoleBean role) {
		strRoles = Util.removeString(strRoles, role.getRolename(),",");
	}

	/*public void updateRolesFromStr(TomcatUsersBean tomcatUsers) {
		String[] tab = strRoles.split(",");
		for (int i = 0; i < tab.length; i++) {
			RoleBean role = tomcatUsers.getRoleByName(tab[i]);
			if (role != null) {
				addRoleBean(role);
			}
		}
	}*/

	/*public void updateRolesFromList(TomcatUsersBean tomcatUsers) {
		strRoles = "";
		for (RoleBean role : getRoles()) {
			strRoles = Util.addString(strRoles, role.getRolename(),",");
		}
	}*/
	
	public String save()
	{
		TomcatUsersBean tomcatUsersBean =(TomcatUsersBean) XmlManager.getInstance().getObjectXml();
		String validation="";
		
		if (id!=""){
			/*Update UserBean*/
			validation= Validator.getInstance().validateEditUser(id);
			if( validation.equals("")){
			   tomcatUsersBean.getUserByName(id).setUsername(username);
			   tomcatUsersBean.getUserByName(id).setPassword(password);
			   tomcatUsersBean.getUserByName(id).setRoles(roles);
			   tomcatUsersBean.getUserByName(id).setRolesStr(strRoles);
			}else{
				return validation;
			}
		}else{
			/*Add UserBean*/
			validation= Validator.getInstance().validateInsertUser(tomcatUsersBean,id);
			if( validation.equals(""))
			   tomcatUsersBean.addUser(this);
			else
				return validation;
		}
		tomcatUsersBean.save();
	    return "Saved";
		 
	}
	
	public String delete()
	{
		String validation = Validator.getInstance().validateDeleteUser(id);
		if( validation.equals(""))
		{
		   TomcatUsersBean tomcatUsersBean	=(TomcatUsersBean) XmlManager.getInstance().getObjectXml();
		   tomcatUsersBean.removeUser(tomcatUsersBean.getUserByName(id));
		   tomcatUsersBean.save();
		   return "Deleted";
		}
		return validation;
	}
	
}
